package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsCoupon;
import com.qf.service.DtsCouponService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lixu
 */
@RestController
@RequestMapping("/admin/coupon")
@CrossOrigin
public class CouponController {

    @Autowired
    private DtsCouponService dtsCouponService;

    /**
     * ### 推广管理
     *
     * #### 2.3.1. 优惠劵管理
     *
     * ##### 2.3.1.1. 优惠劵列表
     *
     * - 接口地址 : http://localhost:8083/admin/coupon/list?page=1&limit=20&sort=add_time&order=desc
     *
     * - 下行数据 :
     */
    @RequiresPermissionsDesc(menu = {"推广管理","优惠劵管理"},button = "查询")
    @RequiresPermissions(value = "admin:coupon:list")
    @GetMapping("/list")
    public Object list(String name,Integer goodsType, Integer status, String sort, String order,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit
    ){
        //分页优惠券
        IPage<DtsCoupon> iPage = dtsCouponService.findListByQuery(name,goodsType,status,sort,order,page,limit);
        Map data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());
        return ResponseUtil.ok(data);
    }

    /**
     * /create
     */
    @RequiresPermissions(value = "admin:coupon:create")
    @RequiresPermissionsDesc(menu = {"推广管理","优惠劵管理"},button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody DtsCoupon dtsCoupon){
        dtsCoupon = dtsCouponService.create(dtsCoupon);
        return ResponseUtil.ok(dtsCoupon);

    }

    /**
     * update
     */
    @RequiresPermissions(value = "admin:coupon:update")
    @RequiresPermissionsDesc(menu = {"推广管理","优惠劵管理"},button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody DtsCoupon dtsCoupon){
        dtsCoupon = dtsCouponService.update(dtsCoupon);
        return ResponseUtil.ok(dtsCoupon);

    }

    /**
     * delete
     */
    @RequiresPermissions(value = "admin:coupon:delete")
    @RequiresPermissionsDesc(menu = {"推广管理","优惠劵管理"},button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsCoupon dtsCoupon){
        dtsCoupon = dtsCouponService.delete(dtsCoupon);
        return ResponseUtil.ok(dtsCoupon);

    }
}
